package com.mathlearning.auth.controller;

import com.mathlearning.auth.common.ApiResponse;
import com.mathlearning.auth.dto.learning.admin.*;
import com.mathlearning.auth.service.LearningAdminService;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

@RestController
@RequestMapping("/api/learning/admin")
@Slf4j
public class LearningAdminController {
    
    @Autowired
    private LearningAdminService learningAdminService;
    
    // ==================== 分类管理 ====================
    
    /**
     * 创建课程分类
     */
    @PostMapping("/categories")
    public ApiResponse<CategoryResponse> createCategory(
            @RequestBody CreateCategoryRequest request) {
        CategoryResponse response = learningAdminService.createCategory(request);
        return ApiResponse.success("创建成功", response);
    }
    
    // ==================== 课程管理 ====================
    
    /**
     * 创建课程
     */
    @PostMapping("/courses")
    public ApiResponse<CourseResponse> createCourse(
            @RequestBody CreateCourseRequest request,
            HttpServletRequest httpRequest) {
        Long userId = (Long) httpRequest.getAttribute("userId");
        CourseResponse response = learningAdminService.createCourse(request, userId);
        return ApiResponse.success("创建成功", response);
    }
    
    /**
     * 更新课程
     */
    @PutMapping("/courses/{courseId}")
    public ApiResponse<CourseResponse> updateCourse(
            @PathVariable Long courseId,
            @RequestBody UpdateCourseRequest request) {
        CourseResponse response = learningAdminService.updateCourse(courseId, request);
        return ApiResponse.success("更新成功", response);
    }
    
    /**
     * 删除课程
     */
    @DeleteMapping("/courses/{courseId}")
    public ApiResponse<Void> deleteCourse(@PathVariable Long courseId) {
        learningAdminService.deleteCourse(courseId);
        return ApiResponse.success("删除成功", null);
    }
    
    // ==================== 章节管理 ====================
    
    /**
     * 创建章节
     */
    @PostMapping("/courses/{courseId}/chapters")
    public ApiResponse<ChapterResponse> createChapter(
            @PathVariable Long courseId,
            @RequestBody CreateChapterRequest request) {
        ChapterResponse response = learningAdminService.createChapter(courseId, request);
        return ApiResponse.success("创建成功", response);
    }
    
    // ==================== 视频管理 ====================
    
    /**
     * 创建视频
     */
    @PostMapping("/chapters/{chapterId}/videos")
    public ApiResponse<VideoResponse> createVideo(
            @PathVariable Long chapterId,
            @RequestBody CreateVideoRequest request) {
        VideoResponse response = learningAdminService.createVideo(chapterId, request);
        return ApiResponse.success("创建成功", response);
    }
    
    /**
     * 上传视频文件
     */
    @PostMapping("/videos/upload")
    public ApiResponse<VideoUploadResponse> uploadVideo(
            @RequestParam("file") MultipartFile file) {
        VideoUploadResponse response = learningAdminService.uploadVideo(file);
        return ApiResponse.success("上传成功", response);
    }
    
    /**
     * 上传课程封面
     */
    @PostMapping("/courses/upload-cover")
    public ApiResponse<CoverUploadResponse> uploadCover(
            @RequestParam("file") MultipartFile file) {
        CoverUploadResponse response = learningAdminService.uploadCover(file);
        return ApiResponse.success("上传成功", response);
    }
}



